<template>
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
  offset="50"
  :immediate-check="false"
>
    <ArticleItem v-for="obj in searchResultList" :key="obj.art_id" :closeShow="false"  :item="obj" />
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem.vue'
import { searchResultListAPI } from '@/api'
import { getFormateTime } from '@/utils/date'
export default {
  name: 'SearchResult',
  data () {
    return {
      searchResultList: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  components: {
    ArticleItem
  },
  methods: {
    onLoad () {
      this.page++
      if (this.searchResultList.length > 0) {
        this.getAllSearchResult()
      }
    },
    async getAllSearchResult () {
      const res = await searchResultListAPI({
        q: this.$route.params.keywords,
        page: this.page
      })
      res.data.data.results.forEach(item => { item.pubdate = getFormateTime(item.pubdate) })
      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      console.log(res)
      this.searchResultList = [...this.searchResultList, ...res.data.data.results]
      this.loading = false
    }

  },
  created () {
    this.getAllSearchResult()
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
